package com.example.bysj_sb.dao;

import com.example.bysj_sb.domain.*;
import com.example.bysj_sb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository("graduateProjectCacheDao")
public class GraduateProjectCacheDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private GraduateProjectCategoryService graduateProjectCategoryService;
    @Autowired
    private GraduateProjectSourceService graduateProjectSourceService;
    @Autowired
    private GraduateProjectStatusService graduateProjectStatusService;
    @Autowired
    private GraduateProjectTruenessService graduateProjectTruenessService;
    @Autowired
    private GraduateProjectTypeService graduateProjectTypeService;
    @Autowired
    private TeacherService teacherService;
    //增加
    public boolean add(GraduateProject graduateProject) {
        //sql语句
        String sql = "INSERT INTO GraduateProject (title,graduateProjectCategory_id," +
                "graduateProjectType_id,graduateProjectStatus_id,graduateProjectSource_id,graduateProjectTrueness_id," +
                "teacher_id) VALUES (?,?,?,?,?,?,?)";
        //执行sql语句
        int affectedRowNum = jdbcTemplate.update(sql, graduateProject.getTitle(),
                graduateProject.getGraduateProjectCategory().getId(),
                graduateProject.getGraduateProjectType().getId(),graduateProject.getGraduateProjectStatus().getId(),
                graduateProject.getGraduateProjectSource().getId(),graduateProject.getGraduateProjectTrueness().getId()
                ,graduateProject.getTeacher().getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    public List<GraduateProject> findByTeacherId(int teacherId){
        //sql语句
        String sql = "SELECT * FROM GraduateProject where teacher_id=?";
        List<GraduateProject> graduateProjects = jdbcTemplate.query(sql,new Object[]{teacherId} ,new GraduateProjectCacheRowMapper());
        return graduateProjects;
    }

    //通过定义内部类来返回查询结果
    public class GraduateProjectCacheRowMapper implements RowMapper<GraduateProject> {
        @Override
        public GraduateProject mapRow(ResultSet resultSet, int i) throws SQLException {
            //将查询结果赋值给对象
            GraduateProject graduateProject = new GraduateProject();
            graduateProject.setId(resultSet.getInt("id"));
            graduateProject.setTitle(resultSet.getString("title"));

            GraduateProjectCategory graduateProjectCategory =
                graduateProjectCategoryService.find (resultSet.getInt("graduateProjectCategory_id"));
            graduateProject.setGraduateProjectCategory(graduateProjectCategory);

            GraduateProjectType graduateProjectType =
                graduateProjectTypeService.find(resultSet.getInt("graduateProjectType_id"));
            graduateProject.setGraduateProjectType(graduateProjectType);

            graduateProjectStatus graduateProjectStatus =
                graduateProjectStatusService.find(resultSet.getInt("graduateProjectStatus_id"));
            graduateProject.setGraduateProjectStatus(graduateProjectStatus);

            GraduateProjectSource graduateProjectSource =
                graduateProjectSourceService.find(resultSet.getInt("graduateProjectSource_id"));
            graduateProject.setGraduateProjectSource(graduateProjectSource);

            GraduateProjectTrueness graduateProjectTrueness =
                graduateProjectTruenessService.find(resultSet.getInt("graduateProjectTrueness_id"));
            graduateProject.setGraduateProjectTrueness(graduateProjectTrueness);
            Teacher teacher =teacherService.find(resultSet.getInt("teacher_id"));
            graduateProject.setTeacher(teacher);
            return graduateProject;
        }
    }
    //修改
    public boolean update(GraduateProject graduateProject) {
        String sql = "UPDATE graduateProject SET title=?,graduateProjectStatus_id=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql, graduateProject.getTitle(),graduateProject.getGraduateProjectStatus().getId(), graduateProject.getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    //根据id删除
    public boolean delete(int id){
        String sql = "DELETE FROM graduateProject WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}
